usbcpower3 2.2.0
Loading...
Searching...
No Matches
usbcpower3.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2026 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
27
28#ifndef USBCPOWER3_H
29#define USBCPOWER3_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_i2c_master.h"
52
58
63
68
73#define USBCPOWER3_PTN_REG_VENDOR_ID 0x00
74#define USBCPOWER3_PTN_REG_PRODUCT_ID 0x02
75#define USBCPOWER3_PTN_REG_DEVICE_ID 0x04
76#define USBCPOWER3_PTN_REG_USBTYPEC_REV 0x06
77#define USBCPOWER3_PTN_REG_USBPD_REV_VER 0x08
78#define USBCPOWER3_PTN_REG_PD_INTERFACE_REV 0x0A
79#define USBCPOWER3_PTN_REG_ALERT 0x10
80#define USBCPOWER3_PTN_REG_ALERT_MASK 0x12
81#define USBCPOWER3_PTN_REG_POWER_STATUS_MASK 0x14
82#define USBCPOWER3_PTN_REG_FAULT_STATUS_MASK 0x15
83#define USBCPOWER3_PTN_REG_EXTENDED_STATUS_MASK 0x16
84#define USBCPOWER3_PTN_REG_ALERT_EXTENDED_MASK 0x17
85#define USBCPOWER3_PTN_REG_CONFIGURE_STANDARD_OUTPUT 0x18
86#define USBCPOWER3_PTN_REG_TCPC_CONTROL 0x19
87#define USBCPOWER3_PTN_REG_ROLE_CONTROL 0x1A
88#define USBCPOWER3_PTN_REG_FAULT_CONTROL 0x1B
89#define USBCPOWER3_PTN_REG_POWER_CONTROL 0x1C
90#define USBCPOWER3_PTN_REG_CC_STATUS 0x1D
91#define USBCPOWER3_PTN_REG_POWER_STATUS 0x1E
92#define USBCPOWER3_PTN_REG_FAULT_STATUS 0x1F
93#define USBCPOWER3_PTN_REG_EXTENDED_STATUS 0x20
94#define USBCPOWER3_PTN_REG_ALERT_EXTENDED 0x21
95#define USBCPOWER3_PTN_REG_COMMAND 0x23
96#define USBCPOWER3_PTN_REG_DEVICE_CAPABILITIES_1 0x24
97#define USBCPOWER3_PTN_REG_DEVICE_CAPABILITIES_2 0x26
98#define USBCPOWER3_PTN_REG_STANDARD_INPUT_CAPABILITIES 0x28
99#define USBCPOWER3_PTN_REG_STANDARD_OUTPUT_CAPABILITIES 0x29
100#define USBCPOWER3_PTN_REG_CONFIGURE_EXTENDED1 0x2A
101#define USBCPOWER3_PTN_REG_GENERIC_TIMER 0x2C
102#define USBCPOWER3_PTN_REG_MESSAGE_HEADER_INFO 0x2E
103#define USBCPOWER3_PTN_REG_RECEIVE_DETECT 0x2F
104#define USBCPOWER3_PTN_REG_READABLE_BYTE_COUNT 0x30
105#define USBCPOWER3_PTN_REG_TRANSMIT 0x50
106#define USBCPOWER3_PTN_REG_I2C_WRITE_BYTE_COUNT 0x51
107#define USBCPOWER3_PTN_REG_VBUS_VOLTAGE 0x70
108#define USBCPOWER3_PTN_REG_VBUS_SINK_DISCONNECT_THLD 0x72
109#define USBCPOWER3_PTN_REG_VBUS_STOP_DISCHARGE_THLD 0x74
110#define USBCPOWER3_PTN_REG_VBUS_VOLTAGE_ALARM_HI_CFG 0x76
111#define USBCPOWER3_PTN_REG_VBUS_VOLTAGE_ALARM_LO_CFG 0x78
112#define USBCPOWER3_PTN_REG_VBUS_HV_TARGET 0x7A
113#define USBCPOWER3_PTN_REG_EXT_CFG_ID 0x80
114#define USBCPOWER3_PTN_REG_EXT_ALERT 0x82
115#define USBCPOWER3_PTN_REG_EXT_ALERT_MASK 0x84
116#define USBCPOWER3_PTN_REG_EXT_CONFIG 0x86
117#define USBCPOWER3_PTN_REG_EXT_FAULT_CONFIG 0x88
118#define USBCPOWER3_PTN_REG_EXT_CONTROL 0x8E
119#define USBCPOWER3_PTN_REG_EXT_STATUS 0x90
120#define USBCPOWER3_PTN_REG_EXT_GPIO_CONFIG 0x92
121#define USBCPOWER3_PTN_REG_EXT_GPIO_CONTROL 0x93
122#define USBCPOWER3_PTN_REG_EXT_GPIO_ALERT_CONFIG 0x94
123#define USBCPOWER3_PTN_REG_EXT_GPIO_STATUS 0x96
124#define USBCPOWER3_PTN_REG_SOURCE_HIGH_VOLTAGE_MB4B_T 0x97
125#define USBCPOWER3_PTN_REG_ADC_FILTER_CONTROL_1 0x9A
126#define USBCPOWER3_PTN_REG_ADC_FILTER_CONTROL_2 0x9B
127#define USBCPOWER3_PTN_REG_VCONN_CONFIG 0x9C
128#define USBCPOWER3_PTN_REG_VCONN_FAULT_DEBOUNCE 0x9D
129#define USBCPOWER3_PTN_REG_VCONN_FAULT_RECOVERY 0x9E
130#define USBCPOWER3_PTN_REG_VCONN_FAULT_ATTEMPTS 0x9F
131
136#define USBCPOWER3_ACT_REG_MASTER_CONTROL_1 0x00
137#define USBCPOWER3_ACT_REG_MASTER_CONTROL_2 0x01
138#define USBCPOWER3_ACT_REG_GENERAL_STATUS 0x02
139#define USBCPOWER3_ACT_REG_THERMAL_STATUS 0x03
140#define USBCPOWER3_ACT_REG_FAULT_1 0x05
141#define USBCPOWER3_ACT_REG_FAULT_2 0x06
142#define USBCPOWER3_ACT_REG_ADC_OUTPUT_1 0x07
143#define USBCPOWER3_ACT_REG_ADC_OUTPUT_2 0x08
144#define USBCPOWER3_ACT_REG_ADC_CONFIG_1 0x09
145#define USBCPOWER3_ACT_REG_ADC_CONFIG_2 0x0A
146#define USBCPOWER3_ACT_REG_VREG_CONTROL 0x0B
147#define USBCPOWER3_ACT_REG_CONVERTER_CONTROL_1 0x0E
148#define USBCPOWER3_ACT_REG_CONVERTER_CONTROL_2 0x0F
149#define USBCPOWER3_ACT_REG_CONVERTER_CONTROL_3 0x10
150#define USBCPOWER3_ACT_REG_VREG_VOLTAGE 0x11
151#define USBCPOWER3_ACT_REG_OUTPUT_VOLTAGE_1 0x13
152#define USBCPOWER3_ACT_REG_OUTPUT_VOLTAGE_2 0x14
153#define USBCPOWER3_ACT_REG_OUTPUT_CURRENT_LIMIT 0x17
154#define USBCPOWER3_ACT_REG_VIN_UV 0x1A
155#define USBCPOWER3_ACT_REG_FREQUENCY 0x1D
156#define USBCPOWER3_ACT_REG_IRQ_CONTROL_1 0x1E
157#define USBCPOWER3_ACT_REG_IRQ_CONTROL_2 0x1F
158#define USBCPOWER3_ACT_REG_VOUT_STATUS 0x20
159 // usbcpower3_reg
161
166
171
176#define USBCPOWER3_PTN_VENDOR_ID 0x1FC9
177#define USBCPOWER3_PTN_PRODUCT_ID 0x5110
178
183#define USBCPOWER3_PTN_POWER_CONTROL_FRS_DIS 0x00
184#define USBCPOWER3_PTN_POWER_CONTROL_FRS_EN 0x80
185#define USBCPOWER3_PTN_POWER_CONTROL_FRS_MASK 0x80
186#define USBCPOWER3_PTN_POWER_CONTROL_VBUS_V_MON_EN 0x00
187#define USBCPOWER3_PTN_POWER_CONTROL_VBUS_V_MON_DIS 0x40
188#define USBCPOWER3_PTN_POWER_CONTROL_VBUS_V_MON_MASK 0x40
189#define USBCPOWER3_PTN_POWER_CONTROL_DIS_V_ALARM_EN 0x00
190#define USBCPOWER3_PTN_POWER_CONTROL_DIS_V_ALARM_DIS 0x20
191#define USBCPOWER3_PTN_POWER_CONTROL_DIS_V_ALARM_MASK 0x20
192#define USBCPOWER3_PTN_POWER_CONTROL_AUTO_DISCH_DIS 0x00
193#define USBCPOWER3_PTN_POWER_CONTROL_AUTO_DISCH_EN 0x10
194#define USBCPOWER3_PTN_POWER_CONTROL_AUTO_DISCH_MASK 0x10
195#define USBCPOWER3_PTN_POWER_CONTROL_BLEED_DISCH_DIS 0x00
196#define USBCPOWER3_PTN_POWER_CONTROL_BLEED_DISCH_EN 0x08
197#define USBCPOWER3_PTN_POWER_CONTROL_BLEED_DISCH_MASK 0x08
198#define USBCPOWER3_PTN_POWER_CONTROL_FORCE_DISCH_DIS 0x00
199#define USBCPOWER3_PTN_POWER_CONTROL_FORCE_DISCH_EN 0x04
200#define USBCPOWER3_PTN_POWER_CONTROL_FORCE_DISCH_MASK 0x04
201#define USBCPOWER3_PTN_POWER_CONTROL_VCONN_1W 0x00
202#define USBCPOWER3_PTN_POWER_CONTROL_VCONN_DEV_CAP 0x02
203#define USBCPOWER3_PTN_POWER_CONTROL_VCONN_MASK 0x02
204#define USBCPOWER3_PTN_POWER_CONTROL_VCONN_SRC_DIS 0x00
205#define USBCPOWER3_PTN_POWER_CONTROL_VCONN_SRC_EN 0x01
206#define USBCPOWER3_PTN_POWER_CONTROL_VCONN_SRC_MASK 0x01
207
212#define USBCPOWER3_PTN_CC_STATUS_LOOKING_4_CONNECTION 0x20
213#define USBCPOWER3_PTN_CC_STATUS_CONNECT_RESULT_SRC 0x00
214#define USBCPOWER3_PTN_CC_STATUS_CONNECT_RESULT_SINK 0x10
215#define USBCPOWER3_PTN_CC_STATUS_CONNECT_RESULT_MASK 0x10
216#define USBCPOWER3_PTN_CC_STATUS_CC2_OPEN 0x00
217#define USBCPOWER3_PTN_CC_STATUS_CC2_SRC_RA 0x04
218#define USBCPOWER3_PTN_CC_STATUS_CC2_SRC_RD 0x08
219#define USBCPOWER3_PTN_CC_STATUS_CC2_SINK_DEFAULT 0x04
220#define USBCPOWER3_PTN_CC_STATUS_CC2_SINK_POWER_1_5 0x08
221#define USBCPOWER3_PTN_CC_STATUS_CC2_SINK_POWER_3_0 0x0C
222#define USBCPOWER3_PTN_CC_STATUS_CC2_MASK 0x0C
223#define USBCPOWER3_PTN_CC_STATUS_CC1_OPEN 0x00
224#define USBCPOWER3_PTN_CC_STATUS_CC1_SRC_RA 0x01
225#define USBCPOWER3_PTN_CC_STATUS_CC1_SRC_RD 0x02
226#define USBCPOWER3_PTN_CC_STATUS_CC1_SINK_DEFAULT 0x01
227#define USBCPOWER3_PTN_CC_STATUS_CC1_SINK_POWER_1_5 0x02
228#define USBCPOWER3_PTN_CC_STATUS_CC1_SINK_POWER_3_0 0x03
229#define USBCPOWER3_PTN_CC_STATUS_CC1_MASK 0x03
230
235#define USBCPOWER3_PTN_POWER_STATUS_DEBUG_CONNECTED 0x80
236#define USBCPOWER3_PTN_POWER_STATUS_TCPC_INIT 0x40
237#define USBCPOWER3_PTN_POWER_STATUS_SRC_HIGH_V 0x20
238#define USBCPOWER3_PTN_POWER_STATUS_SRC_VBUS_EN 0x10
239#define USBCPOWER3_PTN_POWER_STATUS_VBUS_DET_EN 0x08
240#define USBCPOWER3_PTN_POWER_STATUS_VBUS_PRESENT 0x04
241#define USBCPOWER3_PTN_POWER_STATUS_VCONN_PRESENT 0x02
242#define USBCPOWER3_PTN_POWER_STATUS_SINKING_VBUS 0x01
243
248#define USBCPOWER3_PTN_COMMAND_WAKE_I2C 0x11
249#define USBCPOWER3_PTN_COMMAND_DIS_VBUS_DETECT 0x22
250#define USBCPOWER3_PTN_COMMAND_EN_VBUS_DETECT 0x33
251#define USBCPOWER3_PTN_COMMAND_DIS_SINK_VBUS 0x44
252#define USBCPOWER3_PTN_COMMAND_EN_SINK_VBUS 0x55
253#define USBCPOWER3_PTN_COMMAND_DIS_SRC_VBUS 0x66
254#define USBCPOWER3_PTN_COMMAND_SRC_VBUS_DEF_V 0x77
255#define USBCPOWER3_PTN_COMMAND_SRC_VBUS_HIGH_V 0x88
256#define USBCPOWER3_PTN_COMMAND_LOOK_4_CONNECTION 0x99
257#define USBCPOWER3_PTN_COMMAND_RX_ONE_MORE 0xAA
258#define USBCPOWER3_PTN_COMMAND_SEND_FRS_SIGNAL 0xCC
259#define USBCPOWER3_PTN_COMMAND_RESET_TRANSMIT_BUFFER 0xDD
260#define USBCPOWER3_PTN_COMMAND_RESET_RECEIVE_BUFFER 0xEE
261#define USBCPOWER3_PTN_COMMAND_I2C_IDLE 0xFF
262
267#define USBCPOWER3_PTN_TIMEOUT_MS 5000
268
274#define USBCPOWER3_DEVICE_ADDRESS_PTN_GND 0x50
275#define USBCPOWER3_DEVICE_ADDRESS_PTN_10K_BYPASS 0x51
276#define USBCPOWER3_DEVICE_ADDRESS_PTN_UNCONNECTED 0x52
277#define USBCPOWER3_DEVICE_ADDRESS_PTN_100K_BYPASS 0x53
278#define USBCPOWER3_DEVICE_ADDRESS_PTN USBCPOWER3_DEVICE_ADDRESS_PTN_GND
279#define USBCPOWER3_DEVICE_ADDRESS_ACT 0x24
280 // usbcpower3_set
282
287
292
297#define USBCPOWER3_MAP_MIKROBUS( cfg, mikrobus ) \
298 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
299 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
300 cfg.fen = MIKROBUS( mikrobus, MIKROBUS_AN ); \
301 cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
302 cfg.lim = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
303 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
304 // usbcpower3_map // usbcpower3
307
312typedef struct
313{
314 uint16_t vendor_id;
315 uint16_t product_id;
316 uint16_t device_id;
317 uint16_t usbtypec_rev;
318 uint16_t usbpd_rev_ver;
320
322
327typedef struct
328{
329 // Output pins
330 digital_out_t en;
331 digital_out_t lim;
332
333 // Input pins
334 digital_in_t fen;
335 digital_in_t int_pin;
336
337 // Modules
338 i2c_master_t i2c;
339
340 // I2C slave address
342
344
346
351typedef struct
352{
353 pin_name_t scl;
354 pin_name_t sda;
355
356 pin_name_t fen;
357 pin_name_t en;
358 pin_name_t lim;
359 pin_name_t int_pin;
360
361 uint32_t i2c_speed;
362 uint8_t i2c_address;
363
365
376
382
393
408
421err_t usbcpower3_ptn_write_reg_byte ( usbcpower3_t *ctx, uint8_t reg, uint8_t data_in );
422
435err_t usbcpower3_ptn_read_reg_byte ( usbcpower3_t *ctx, uint8_t reg, uint8_t *data_out );
436
449err_t usbcpower3_ptn_write_reg_word ( usbcpower3_t *ctx, uint8_t reg, uint16_t data_in );
450
463err_t usbcpower3_ptn_read_reg_word ( usbcpower3_t *ctx, uint8_t reg, uint16_t *data_out );
464
478err_t usbcpower3_ptn_write_reg_block ( usbcpower3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len );
479
493err_t usbcpower3_ptn_read_reg_block ( usbcpower3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len );
494
507err_t usbcpower3_act_write_reg ( usbcpower3_t *ctx, uint8_t reg, uint8_t data_in );
508
521err_t usbcpower3_act_read_reg ( usbcpower3_t *ctx, uint8_t reg, uint8_t *data_out );
522
534
544
554
564
576
586
596
606
616
617#ifdef __cplusplus
618}
619#endif
620#endif // USBCPOWER3_H
621 // usbcpower3
623
624// ------------------------------------------------------------------------ END
err_t usbcpower3_init(usbcpower3_t *ctx, usbcpower3_cfg_t *cfg)
USB-C Power 3 initialization function.
err_t usbcpower3_ptn_read_reg_word(usbcpower3_t *ctx, uint8_t reg, uint16_t *data_out)
USB-C Power 3 PTN read register word function.
void usbcpower3_restart_device(usbcpower3_t *ctx)
USB-C Power 3 device restart function.
uint8_t usbcpower3_get_fen_pin(usbcpower3_t *ctx)
USB-C Power 3 get FEN pin function.
err_t usbcpower3_wait_tcpc_init(usbcpower3_t *ctx)
USB-C Power 3 wait TCPC init function.
err_t usbcpower3_act_write_reg(usbcpower3_t *ctx, uint8_t reg, uint8_t data_in)
USB-C Power 3 ACT write register function.
err_t usbcpower3_act_read_reg(usbcpower3_t *ctx, uint8_t reg, uint8_t *data_out)
USB-C Power 3 ACT read register function.
void usbcpower3_set_low_current(usbcpower3_t *ctx)
USB-C Power 3 set low current function.
err_t usbcpower3_ptn_write_reg_byte(usbcpower3_t *ctx, uint8_t reg, uint8_t data_in)
USB-C Power 3 PTN write register byte function.
err_t usbcpower3_ptn_read_reg_byte(usbcpower3_t *ctx, uint8_t reg, uint8_t *data_out)
USB-C Power 3 PTN read register byte function.
err_t usbcpower3_ptn_write_reg_block(usbcpower3_t *ctx, uint8_t reg, uint8_t *data_in, uint8_t len)
USB-C Power 3 PTN write register block function.
err_t usbcpower3_ptn_write_reg_word(usbcpower3_t *ctx, uint8_t reg, uint16_t data_in)
USB-C Power 3 PTN write register word function.
void usbcpower3_enable_device(usbcpower3_t *ctx)
USB-C Power 3 device enable function.
void usbcpower3_set_high_current(usbcpower3_t *ctx)
USB-C Power 3 set high current function.
err_t usbcpower3_ptn_read_reg_block(usbcpower3_t *ctx, uint8_t reg, uint8_t *data_out, uint8_t len)
USB-C Power 3 PTN read register block function.
err_t usbcpower3_ptn_read_info(usbcpower3_t *ctx)
USB-C Power 3 PTN read device information function.
void usbcpower3_disable_device(usbcpower3_t *ctx)
USB-C Power 3 device disable function.
void usbcpower3_cfg_setup(usbcpower3_cfg_t *cfg)
USB-C Power 3 configuration object setup function.
uint8_t usbcpower3_get_int_pin(usbcpower3_t *ctx)
USB-C Power 3 get interrupt pin function.
USB-C Power 3 Click configuration object.
Definition usbcpower3.h:352
uint32_t i2c_speed
Definition usbcpower3.h:361
pin_name_t fen
Definition usbcpower3.h:356
pin_name_t scl
Definition usbcpower3.h:353
pin_name_t en
Definition usbcpower3.h:357
pin_name_t int_pin
Definition usbcpower3.h:359
pin_name_t sda
Definition usbcpower3.h:354
pin_name_t lim
Definition usbcpower3.h:358
uint8_t i2c_address
Definition usbcpower3.h:362
USB-C Power 3 Click PTN info object.
Definition usbcpower3.h:313
uint16_t product_id
Definition usbcpower3.h:315
uint16_t pd_interface_rev
Definition usbcpower3.h:319
uint16_t usbpd_rev_ver
Definition usbcpower3.h:318
uint16_t usbtypec_rev
Definition usbcpower3.h:317
uint16_t vendor_id
Definition usbcpower3.h:314
uint16_t device_id
Definition usbcpower3.h:316
USB-C Power 3 Click context object.
Definition usbcpower3.h:328
digital_out_t lim
Definition usbcpower3.h:331
usbcpower3_ptn_info_t info
Definition usbcpower3.h:343
digital_in_t int_pin
Definition usbcpower3.h:335
i2c_master_t i2c
Definition usbcpower3.h:338
digital_out_t en
Definition usbcpower3.h:330
uint8_t slave_address
Definition usbcpower3.h:341
digital_in_t fen
Definition usbcpower3.h:334
usbcpower3_return_value_t
USB-C Power 3 Click return value data.
Definition usbcpower3.h:371
@ USBCPOWER3_OK
Definition usbcpower3.h:372
@ USBCPOWER3_ERROR
Definition usbcpower3.h:373